package com.chapp.provider.handler;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.chapp.base.Location;
import com.chapp.provider.DroidContentProvider;

public class LocationHandler {
	
	public static final Uri CONTENT_URI = Uri.parse("content://" + DroidContentProvider.AUTHORITY
			+ "/location");
	public static final String ID = "_id";
	public static final String CITY = "city";
	public static final String COUNTRY = "country";
	public static final String STREET = "street";
	public static final String POSTALCODE = "postalcode";
	public static final String LAT = "lat";
	public static final String LONG = "long";
	
	public static Uri insertLocation(Context context, Location location) {
		ContentValues valores = new ContentValues();
		valores.put(CITY, location.getCity());
		valores.put(COUNTRY, location.getCountry());
		valores.put(LAT, location.getGeoLat());
		valores.put(LONG, location.getGeoLong());
		valores.put(POSTALCODE, location.getPostalCode());
		valores.put(STREET, location.getStreet());

		Uri uri = context.getContentResolver().insert(CONTENT_URI, valores);
		
		Log.i(uri.getPath(), uri.getPath());
		return uri;
	}

	public static Location getLocation(Context context, int id) {
		Cursor cursor = context.getContentResolver().query(CONTENT_URI, null, "_id = " + id, null,
				null);
		Location location = new Location();
		if (cursor.moveToNext()) {
			location.setCity(cursor.getString(cursor.getColumnIndex(LocationHandler.CITY)));
			location.setCountry(cursor.getString(cursor.getColumnIndex(LocationHandler.COUNTRY)));
			location.setGeoLat(cursor.getString(cursor.getColumnIndex(LocationHandler.LAT)));
			location.setGeoLong(cursor.getString(cursor.getColumnIndex(LocationHandler.LONG)));
			location.setPostalCode(cursor.getString(cursor.getColumnIndex(LocationHandler.POSTALCODE)));
			location.setStreet(cursor.getString(cursor.getColumnIndex(LocationHandler.STREET)));
		}
		cursor.close();
		return location;
	}

	public static boolean deleteLocation(Context context, int id) {
		int rows = context.getContentResolver().delete(CONTENT_URI, "_id = " + id, null);
		if (rows != 0) {
			return true;
		}
		return false;
	}

}
